﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BoggleGUI
{
    /// <summary>
    /// Interaction logic for BoggleCell.xaml
    /// </summary>
    public partial class BoggleCell : UserControl
    {
        private Tuple<int, int> coordinates;

        public delegate void CellClickedHandler(object sender, CellEventArgs e);
        public event CellClickedHandler CellClicked;

        //SolidColorBrush maroonBrush = new SolidColorBrush(Colors.Maroon);
        //SolidColorBrush blackBrush = new SolidColorBrush(Colors.Black);
        //SolidColorBrush grayBrush = new SolidColorBrush(Colors.Gray);

        Brushes brushes = new Brushes();

        //private bool clicked = false;

        public BoggleCell(int c, int r)
        {
            coordinates = new Tuple<int, int>(c, r);            

            InitializeComponent();

            rect1.Stroke = brushes.GreenBrush;
            rect2.Stroke = brushes.GreenBrush;
            rect2.Fill = brushes.GrayBrush;
            pieceLabel.Foreground = brushes.GreenBrush;
        }

        public void SetLabel(char c)
        {
            if (c == 'Q' || c == 'q')
                this.pieceLabel.Content = "" + c + 'u';
            else
                this.pieceLabel.Content = c;
        }

        public string GetLabel()
        {
            return this.pieceLabel.Content.ToString();
        }

        public void ResetPiece()
        {
            //clicked = false;
            this.rect2.Visibility = Visibility.Hidden;
        }

        public Tuple<int, int> GetCoordinate()
        {
            return coordinates;
        }

        private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (CellClicked != null)
            {
                CellClicked(this, new CellEventArgs(coordinates));
            }
        }

        private void UserControl_MouseEnter(object sender, MouseEventArgs e)
        {
            this.rect1.Fill = brushes.MaroonBrush;            
        }

        private void UserControl_MouseLeave(object sender, MouseEventArgs e)
        {
            this.rect1.Fill = brushes.BlackBrush;            
        }

        public bool IsValidClick(BoggleCell other)
        {
            int xDist = Math.Abs(this.coordinates.Item1 - other.coordinates.Item1);
            int yDist = Math.Abs(this.coordinates.Item2 - other.coordinates.Item2);

            if (xDist > 1 || yDist > 1)
                return false;
            else
                return true;
        }
    }

    public class CellEventArgs : EventArgs
    {
        public Tuple<int, int> coords;

        public CellEventArgs(Tuple<int, int> c)
        {
            coords = c;
        }
    }
}
